clear all
set more off
capture log close
log using "$output\table9.log", replace

use "$data/cpsmergeddata.dta", clear

*** total adults
gen pop=(asecwt + spouseasecwt)
egen totadults=sum(pop)
drop pop

egen totkids=sum(newnumkidswt17)
egen totdeeppoorkids=sum(newnumkidswt17*deeppoor)
sum totdeeppoorkids
drop totdeeppoorkids 

** total effect on poverty
gen deeppooradults=(age>=18)*deeppoor*(asecwt+spouseasecwt)
egen totdeeppooradults=sum(deeppooradults)

gen nolongerdeeppooradults=(age>=18)*deeppoor*(deeppoorctc21==0)*(asecwt+spouseasecwt)
egen totnolongerdeeppooradults=sum(nolongerdeeppooradults)

gen stilldeeppooradults=(age>=18)*deeppoor*(deeppoorctc21==1)*(asecwt+spouseasecwt)
egen totstilldeeppooradults=sum(stilldeeppooradults)


* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*avgtax*.75*group
egen totgroup1a=sum(group1)

gen double group2=asecwt*avgtax*.75*group*deeppoor
egen totgroup2a=sum(group2)

gen double group3=asecwt*avgtax*.75*group*(deeppoor==0)
egen totgroup3a=sum(group3)

gen double group4=asecwt*avgtax*.75*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4a=sum(group4)

gen double group5=asecwt*avgtax*.75*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'a/pop
	}
	
gen totgroup45a=totgroup4a+totgroup5a	
sum pop totgroup1a totgroup2a totgroup45a povnum1a povnum2a povnum3a povrate1 povrate2 povrate3
sum avgtax if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1a-povnum3a)/povnum1a
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=asecwt*avgtax*.25*group*deeppoor
egen totgroup2b=sum(group2)

gen double group3=asecwt*avgtax*.25*group*(deeppoor==0)
egen totgroup3b=sum(group3)

gen double group4=asecwt*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4b=sum(group4)

gen double group5=asecwt*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'b/pop
	}
	
gen totgroup45b=totgroup4b+totgroup5b	
sum pop totgroup1b totgroup2b totgroup45b povnum1b povnum2b povnum3b povrate1 povrate2 povrate3
sum avgtax if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1b-povnum3b)/povnum1b
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=asecwt*avgtax*.25*group*deeppoor
egen totgroup2c=sum(group2)

gen double group3=asecwt*avgtax*.25*group*(deeppoor==0)
egen totgroup3c=sum(group3)

gen double group4=asecwt*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4c=sum(group4)

gen double group5=asecwt*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'c/pop
	}

gen totgroup45c=totgroup4c+totgroup5c	
sum pop totgroup1c totgroup2c totgroup45c povnum1c povnum2c povnum3c povrate1 povrate2 povrate3
sum avgtax if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1c-povnum3c)/povnum1c
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=asecwt*newrtwchangespouse*.25*group
egen totgroup1d=sum(group1)

gen double group2=asecwt*newrtwchangespouse*.25*group*deeppoor
egen totgroup2d=sum(group2)

gen double group3=asecwt*newrtwchangespouse*.25*group*(deeppoor==0)
egen totgroup3d=sum(group3)

gen double group4=asecwt*newrtwchangespouse*.25*group*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=asecwt*newrtwchangespouse*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/asecwt+group5/asecwt)/(group)

egen pop=sum(group*asecwt)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*asecwt)
	gen povrate`i'=povnum`i'd/pop
	}
	
gen totgroup45d=totgroup4d+totgroup5d	
sum pop totgroup1d totgroup2d totgroup45d povnum1d povnum2d povnum3d povrate1 povrate2 povrate3
sum newrtwchangespouse if group==1 [aw=asecwt]

di "pov % decrease="
di (povnum1d-povnum3d)/povnum1d
drop pov1 pov2 pov3 povrate* pop group5 group*


* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(asecwt*person1+spouseasecwt*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(asecwt*person1+spouseasecwt*person2)*avgtax*.25*deeppoor
egen totgroup2e=sum(group2)

gen double group3=(asecwt*person1+spouseasecwt*person2)*avgtax*.25*(deeppoor==0)
egen totgroup3e=sum(group3)

gen double group4=(asecwt*person1)*avgtax*.25*(deeppoor==0)*deeppoorctc21nother+(spouseasecwt*person2)*avgtax*.25*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4e=sum(group4)

gen double group5=(asecwt*person1)*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother+spouseasecwt*person2*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(deeppoor*(person1*asecwt+person2*spouseasecwt))
gen pov2=(deeppoorctc21*(person1*asecwt+person2*spouseasecwt))
gen pov3=(deeppoorctc21*(person1*asecwt+person2*spouseasecwt)+group4+group5)

egen pop=sum(asecwt*person1+spouseasecwt*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

gen totgroup45e=totgroup4e+totgroup5e	
sum pop totgroup1e totgroup2e totgroup45e povnum1e povnum2e povnum3e povrate1 povrate2 povrate3
sum newrtwchangespouse if person1 | person2 [aw=asecwt]

di "pov % decrease="
di (povnum1e-povnum3e)/povnum1e
drop pov1 pov2 pov3 povrate* group5 group* pop

gen pop=(asecwt*(incwage>0) + spouseasecwt*(totincwage>incwage))*(agi2017<80000)
egen totpop=sum(pop)

forvalues i=1/2{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}
di totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e

di totdeeppooradults
di totstilldeeppooradults
di (totstilldeeppooradults+totgroup4a+totgroup4b+2*totgroup4c+2*totgroup4d+totgroup4e+totgroup5a+totgroup5b+2*totgroup5c+2*totgroup5d+totgroup5e)
di totdeeppooradults/totadults
di totstilldeeppooradults/totadults
di (totstilldeeppooradults+totgroup4a+totgroup4b+2*totgroup4c+2*totgroup4d+totgroup4e+totgroup5a+totgroup5b+2*totgroup5c+2*totgroup5d+totgroup5e)/totadults
di "pov % decrease="
di (totdeeppooradults-(totstilldeeppooradults+totgroup4a+totgroup4b+2*totgroup4c+2*totgroup4d+totgroup4e+totgroup5a+totgroup5b+2*totgroup5c+2*totgroup5d+totgroup5e))/totdeeppooradults
drop totgroup* povnum* person* pop totpop


* KIDS 

drop stilldeeppoor totstilldeeppoor 

gen deeppoorkids=deeppoor*(newnumkidswt17)
egen totdeeppoorkids=sum(deeppoorkids)
di totdeeppoorkids

gen stilldeeppoor=deeppoor*(deeppoorctc21==1)*(newnumkidswt17)
egen totstilldeeppoor=sum(stilldeeppoor)

di totkids
di totdeeppoorkids
di totstilldeeppoor

di totdeeppoorkids/totkids
di (totstilldeeppoor)/totkids

* row 1
gen double group=(married==0)*(incwage>0)*(v25>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.75*group
egen totgroup1a=sum(group1)
gen double group2=newnumkidswt17*avgtax*.75*group*deeppoor
egen totgroup2a=sum(group2)

gen double group3=newnumkidswt17*avgtax*.75*group*(deeppoor==0)
egen totgroup3a=sum(group3)

gen double group4=newnumkidswt17*avgtax*.75*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4a=sum(group4)

gen double group5=newnumkidswt17*avgtax*.75*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5a=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'a=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'a/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 2
gen double group=(married==0)*(incwage>0)*(v25==0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1b=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2b=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3b=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4b=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5b=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'b=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'b/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 3
gen double group=(married==1)*(incwage>0)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*avgtax*.25*group
egen totgroup1c=sum(group1)

gen double group2=newnumkidswt17*avgtax*.25*group*deeppoor
egen totgroup2c=sum(group2)

gen double group3=newnumkidswt17*avgtax*.25*group*(deeppoor==0)
egen totgroup3c=sum(group3)

gen double group4=newnumkidswt17*avgtax*.25*group*(deeppoor==0)*deeppoorctc21nother
egen totgroup4c=sum(group4)

gen double group5=newnumkidswt17*avgtax*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother
egen totgroup5c=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'c=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'c/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 4
gen double group=(married==1)*(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplejustwomen==1)

gen double group1=newnumkidswt17*newrtwchangespouse*.25*group
egen totgroup1d=sum(group1)

gen double group2=newnumkidswt17*newrtwchangespouse*.25*group*deeppoor
egen totgroup2d=sum(group2)

gen double group3=newnumkidswt17*newrtwchangespouse*.25*group*(deeppoor==0)
egen totgroup3d=sum(group3)

gen double group4=newnumkidswt17*newrtwchangespouse*.25*group*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4d=sum(group4)

gen double group5=newnumkidswt17*newrtwchangespouse*.25*group*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5d=sum(group5)

gen pov1=(deeppoor*group)/(group)
gen pov2=(deeppoor*group*(deeppoorctc21==1))/(group)
gen pov3=(deeppoor*group*(deeppoorctc21==1)+group4/newnumkidswt17+group5/newnumkidswt17)/(group)

egen pop=sum(group*newnumkidswt17)
forvalues i=1/3{
	egen povnum`i'd=sum(pov`i'*newnumkidswt17)
	gen povrate`i'=povnum`i'd/pop
	}
drop pov1 pov2 pov3 povrate* pop group5 group*

* row 5: sum up non-mom and potential spouse
gen person1=(incwage>0)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)
gen person2=(totincwage>incwage)*(agi2017<80000)*(calckids>0)*(samplenotmoms==1)*(married==1)

gen double group1=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25
egen totgroup1e=sum(group1)

gen double group2=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*deeppoor
egen totgroup2e=sum(group2)

gen double group3=(newnumkidswt17*person1+newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)
egen totgroup3e=sum(group3)

gen double group4=(newnumkidswt17*person1)*avgtax*.25*(deeppoor==0)*deeppoorctc21nother+(newnumkidswt17*person2)*avgtax*.25*(deeppoor==0)*deeppoorctc21notspouse
egen totgroup4e=sum(group4)

gen double group5=(newnumkidswt17*person1)*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21nother+newnumkidswt17*person2*avgtax*.25*(deeppoor==1)*(deeppoorctc21==0)*deeppoorctc21notspouse
egen totgroup5e=sum(group5)

gen pov1=(deeppoor*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov2=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17))
gen pov3=(deeppoorctc21*(person1*newnumkidswt17+person2*newnumkidswt17)+group4+group5)

egen pop=sum(newnumkidswt17*person1+newnumkidswt17*person2)
forvalues i=1/3{
	egen povnum`i'e=sum(pov`i')
	gen povrate`i'=povnum`i'e/pop
	}

drop pov1 pov2 pov3 povrate* pop group5 group*

forvalues i=1/2{
	di totgroup`i'a+totgroup`i'b+totgroup`i'c+totgroup`i'd+totgroup`i'e
}
di totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e

di totkids
di totdeeppoorkids
di totstilldeeppoor
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)
di totdeeppoorkids/totkids
di totstilldeeppoor/totkids
di (totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e)/totkids
di "pov % decrease="
di (totdeeppoorkids-(totstilldeeppoor+totgroup4a+totgroup4b+totgroup4c+totgroup4d+totgroup4e+totgroup5a+totgroup5b+totgroup5c+totgroup5d+totgroup5e))/totdeeppoorkids
drop totgroup* povnum* person*

log close